package colibri.lcdui;


import colibri.App;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Bayron Thiengo Quinelato
 */
public class ChoiceGroup extends Item {

    public static final int VERTICAL = 0;
    public static final int HORIZONTAL = 1;
    private String[] items;
    public boolean isFixed;
    public int itemSelected;
    private int type;
    private int itemWidth;
    private int itemHeight;
    private int numItens;
    private Image imSelect, imNoSelect;
    private Font font;

    public ChoiceGroup(String[] items, int type) {
        super(0, 0, 0, 0, Graphics.TOP | Graphics.LEFT);

        if (items == null) {
            throw new NullPointerException("Colibri error: Falha ao criar ChoiceGroup. O argumento 'items' é nulo.");
        } else if (type != VERTICAL && type != HORIZONTAL) {
            throw new IllegalArgumentException("Colibri error: Falha ao criar ChoiceGroup. O argumento 'type' é inválido.");
        }

        this.items = items;
        this.type = type;

        try {
            imSelect = Image.createImage("/res/img/choicegroup1.png");
            imNoSelect = Image.createImage("/res/img/choicegroup2.png");
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new NullPointerException("Colibri error: Falha ao criar ChoiceGroup."
                    + " As imagens do ChoiceGroup não puderam ser carreadas.");
        }

        itemWidth = imSelect.getWidth() + 2;
        itemHeight = imSelect.getHeight() + 5;
        numItens = items.length;
        itemSelected = 0;
        font = Font.getFont(0);

        if (type == VERTICAL) {
            int lenght = 0;
            int it = 0;
            for (int i = 0; i < numItens; i++) {
                if (items[i].length() > lenght) {
                    lenght = items[i].length();
                    it = i;
                }
            }
            width = itemWidth + font.stringWidth(items[it]) + 4;
            if (font.getHeight() > itemHeight) {
                height = numItens * font.getHeight() + 2;
            } else {
                height = numItens * itemHeight + 4;
            }

        } else if (type == HORIZONTAL) {
            int w = 0;
            for (int i = 0; i < numItens; i++) {
                w += itemWidth + font.stringWidth(items[i]) + 5;
            }
            width = w + 4;
            if (font.getHeight() > itemHeight) {
                height = font.getHeight() + 2;
            } else {
                height = itemHeight + 4;
            }
        }
    }

    protected void callPaint(Graphics g) {
        g.setColor(0x000000);
        g.setFont(font);
        if (type == VERTICAL) {
            for (int i = 0; i < numItens; i++) {
                if (i == itemSelected) {
                    g.drawImage(imSelect, x + 2, y + 4 + i * itemHeight, anchor);
                } else {
                    g.drawImage(imNoSelect, x + 2, y + 4 + i * itemHeight, anchor);
                }
                g.drawString(items[i], x + 2 + itemWidth, y + 1 + i * itemHeight, anchor);
            }

        } else if (type == HORIZONTAL) {
            int itemX = x + 4;
            for (int i = 0; i < numItens; i++) {
                if (i == itemSelected) {
                    g.drawImage(imSelect, itemX, y + 4, anchor);
                } else {
                    g.drawImage(imNoSelect, itemX, y + 4, anchor);
                }
                g.drawString(items[i], itemX + itemWidth, y + 1, anchor);
                itemX += itemWidth + font.stringWidth(items[i]) + 5;
            }
        }

        if (focus) {
            if (isFixed) {
                g.setColor(0x888888);
            }
            g.drawRect(x, y, width - 1, height - 1);
        }
    }

    public String getSelectedItemName() {
        return items[itemSelected];
    }

    public void setFocus(boolean hasFocus) {
        setFocus(hasFocus);
        if (!hasFocus) {
            isFixed = false;
        }
    }

    protected void callKeyReleased(int keyCode) {
        int gameAction = App.getGameAction(keyCode);
        switch (gameAction) {
            case App.FIRE:
                if (isFixed) {
                    isFixed = false;
                } else {
                    isFixed = true;
                }
                break;

            case App.DOWN:
                if (isFixed && type == VERTICAL && itemSelected < numItens - 1) {
                    itemSelected++;
                }
                break;

            case App.UP:
                if (isFixed && type == VERTICAL && itemSelected > 0) {
                    itemSelected--;
                }
                break;

            case App.LEFT:
                if (isFixed && type == HORIZONTAL && itemSelected > 0) {
                    itemSelected--;
                }
                break;

            case App.RIGHT:
                if (isFixed && type == HORIZONTAL && itemSelected < numItens - 1) {
                    itemSelected++;
                }
                break;
        }
    }

    protected void callPointerReleased(int x, int y) {
        if (x > super.x && x < super.x + width && y > y && y < super.y + height) {
            if (type == VERTICAL) {
                for (int i = 0; i < numItens; i++) {
                    if (y > super.y + 4 + i * itemHeight && y < super.y + 4 + (i + 1) * itemHeight) {
                        itemSelected = i;
                    }
                }

            } else if (type == HORIZONTAL) {
                int xAux = x + 4;
                for (int i = 0; i < numItens; i++) {
                    if (x > xAux && x < xAux + itemWidth) {
                        itemSelected = i;
                    }
                    xAux += itemWidth + font.stringWidth(items[i]) + 5;
                }
            }
        }
    }

    public void setPosition(int x, int y) {
        super.x = x;
        super.y = y;
    }
}
